package com.gohome.gohomechat.model;

import android.os.Handler;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.gohome.gohomechat.db.GoHomeDBTools;
import com.gohome.gohomechat.model.entify.Contant;
import com.gohome.gohomechat.model.entify.Notice;
import com.gohome.gohomechat.model.modellistener.NoticeModelInterface;
import com.gohome.gohomechat.presenter.callback.NoticeAgreeCallBack;
import com.gohome.gohomechat.presenter.callback.NoticeCallBack;
import com.gohome.gohomechat.presenter.callback.NoticeRefusedCallBack;

import java.util.List;

/**
 * Created by dllo on 16/3/3.
 */
public class NoticeModel implements NoticeModelInterface {
    private Handler handler = new Handler();


    @Override
    public void requestData(final NoticeCallBack callBack) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                List<Notice> data = GoHomeDBTools.getInstance().queryAllDatas();
                if (data == null)
                    callBack.onFailed();
                else
                    callBack.onScuess(data);
            }
        });

    }

    @Override
    public void agreeFriendRequest(final String userName, final NoticeAgreeCallBack callBack) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //同意username的好友请求
                    EMChatManager.getInstance().acceptInvitation(userName);//需异步处理
                    GoHomeDBTools.getInstance().updateMessageStatus(userName, Contant.MESSAGESTATUS_AGREED);
                    callBack.onScuess();
                } catch (EaseMobException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void refusedFriendRequest(final String userName, final NoticeRefusedCallBack callBack) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    EMChatManager.getInstance().refuseInvitation(userName);//需异步处理
                    GoHomeDBTools.getInstance().updateMessageStatus(userName, Contant.MESSAGESTATUS_REFUSED);
                    callBack.onScuess();
                } catch (EaseMobException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        });

    }
}
